package org.nod.atgm.gameobjects;

import org.nod.atgm.GameManager;

import com.threed.jpct.SimpleVector;

public class CircularPropeller implements IPropeller {
	
	protected final MovingObject object;
	protected final SimpleVector center;
	protected final float radius;
	protected final float speed;
	protected final float startingAngle;
	protected final float maxAngle;
	protected final float y;
	protected float currentAngle;
	protected boolean needRemove = false;
	protected float currentX = 0;
	protected float currentZ = 0;
	protected SimpleVector direction = new SimpleVector(0f,0f,0f);

	public CircularPropeller(MovingObject object, SimpleVector center, float radius, float speed, float startingAngle, float maxAngle,float y) {
		this.object = object;
		this.center = center;
		this.radius = radius;
//		if (this.startingAngle < this.maxAngle)
			this.speed = speed;
//		else
//			this.speed = -speed;
		this.startingAngle = startingAngle;
		currentAngle = startingAngle;
		this.maxAngle = maxAngle;
		this.y = y;
		float initialX = (float) (/*radius + */radius * Math.cos(startingAngle));
		float initialZ = (float) (radius * Math.cos(startingAngle));
		currentX = center.x + initialX;
		currentZ = center.z + initialZ;
		object.translate(currentX,y,currentZ);
	}

	@Override
	public void handleTimer(int time) {
		currentAngle += speed;
		if (speed > 0 && currentAngle < maxAngle || speed < 0 && currentAngle > maxAngle ) {
			float x = (float) (radius * Math.cos(currentAngle)) + center.x;
			float z = (float) (radius * Math.sin(currentAngle)) + center.z;
			direction.x = x - currentX;
			direction.z = z - currentZ;
			object.translate(x - currentX,0,z - currentZ);
			currentX = x;
			currentZ = z;
//			object.getObject().setOrientation(direction, GameManager.INITIAL_VECTOR_UP);
		} else if (object instanceof IRemovable){
			((IRemovable) object).setNeedsToRemove(true);
		}
	}
	

}
